#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <math.h>

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    const string dir = "/home/yjy/dataset/palmprint_dectection_tongji/LHand/palmprint_rotation_adjusted/";
    const string labels_save_path = "/home/yjy/PycharmProjects/deeplab_v3/deeplab/datasets/palmprint_detection/palmprint_segment/";

    string filename;
    dirent *file;
    DIR *m_dir = opendir(dir.c_str());

    while ((file = readdir(m_dir)) != NULL)
    {
        if (strcmp(file->d_name, ".") == 0 ||
            strcmp(file->d_name, "..") == 0)
            continue;
        filename = file->d_name;

        cv::Mat img = cv::imread(dir + filename);
        cv::cvtColor(img, img, CV_BGR2GRAY);
        cv::threshold(img, img, 20, 255, CV_THRESH_BINARY);
        cv::erode(img, img, cv::Mat(), cv::Point(-1, -1), 2);
        // for (int i = 0; i < img.cols; i++)
        // {
        //     for (int j = 0; j < img.rows; j++)
        //     {
        //         if (img.at<uchar>(j, i) == 255)
        //             img.at<uchar>(j, i) = 1;
        //     }
        // }
        cv::imwrite(labels_save_path + filename.substr(0, 17) + ".png", img);
    }

    return 0;
}